package week_three_day03.dom;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class DomDemo03 {
	public static void main(String[] args) throws IOException {
		Element root = new Element("books");
		Document doc = new Document();
		doc.setRootElement(root);
		for(int i = 0;i<10;i++){
			Element bookEle = new Element("book");
			bookEle.setAttribute("id","c"+i);
			Element nameEle = new Element("name");
			nameEle.setText("book"+i);
			Element priceEle = new Element("price");
			priceEle.setText(i+3+"");
			bookEle.addContent(nameEle);
			bookEle.addContent(priceEle);
			
			root.addContent(bookEle);
		}
		Writer writer = new FileWriter("books1.xml");
		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
		outputter.output(doc,writer);
		writer.close();
	}
}
